package com.jsm.tf.controller;

import com.jsm.tf.director.Director;
import com.jsm.tf.exception.BusinessException;
import com.jsm.tf.service.UserService;
import com.jsm.tf.view.LoginView;
import com.jsm.tf.view.dialog.TFDialog;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class RegisterController {
    @Autowired
    private Director director;
    @Autowired
    private UserService userService;

    @FXML
    private TextField password;

    @FXML
    private TextField username;
    @FXML
    private TextField nickname;

    public void returnLogin(MouseEvent mouseEvent) {
        director.showView(LoginView.class);
    }

    public void register(MouseEvent mouseEvent) {
        String username = this.username.getText();
        String password = this.password.getText();
        String nickname = this.nickname.getText();
        try {
            userService.register(username, password, nickname);
            TFDialog.showMessageDialog(GUIState.getStage(),"注册成功","注册成功");
        }catch (BusinessException e){
            TFDialog.showConfirmDialog(GUIState.getStage(),e.getErrorMsg(),"注册失败");
        }
    }
}
